package cn.yuanqiao.web.controller.manage;

import cn.yuanqiao.common.exception.ServiceException;
import cn.yuanqiao.common.utils.poi.ExcelUtil;
import cn.yuanqiao.common.core.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.core.page.TableDataInfo;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.enums.BusinessType;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.manage.domain.YqFields;
import cn.yuanqiao.manage.service.IYqFieldsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * 基础数据Controller
 * 
 * @author yuanqiao
 * @date 2023-06-08
 */
@RestController
@RequestMapping("/manage/yqfields")
public class YqfieldsController extends BaseController
{
    @Autowired
    private IYqFieldsService yqfieldsService;

    /**
     * 查询基础数据列表
     */
    //@RequiresPermissionsrmissions("manage:yqfields:list")
    @Log(title = "模板管理查询基础数据列表",businessType = BusinessType.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(YqFields yqfields)
    {
        startPage();
        List<YqFields> list = yqfieldsService.selectyqfieldsList(yqfields);
        return getDataTable(list);
    }

    /**
     * 导出基础数据列表
     */
    //@RequiresPermissionsrmissions("manage:yqfields:export")
    @Log(title = "模板管理导出基础数据列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YqFields yqfields)
    {
        List<YqFields> list = yqfieldsService.selectyqfieldsList(yqfields);
        ExcelUtil<YqFields> util = new ExcelUtil<YqFields>(YqFields.class);
        util.exportExcel(response, list, "基础数据");
    }

    /**
     * 获取基础数据详细信息
     */
    //@RequiresPermissionsrmissions("manage:yqfields:query")
    @Log(title = "模板管理获取基础数据详细信息", businessType = BusinessType.SELECT)
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") String ID)
    {
        return success(yqfieldsService.selectyqfieldsByID(ID));
    }

    /**
     * 新增基础数据
     */
    //@RequiresPermissionsrmissions("manage:yqfields:add")
    @Log(title = "模板管理新增基础数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody YqFields yqfields)
    {
        String fieldLength = yqfields.getFieldLength();
        String thWidth = yqfields.getThWidth();
        if(Integer.valueOf(fieldLength)<=0) throw new ServiceException("字段长度必须大于0");
        if(thWidth != null && Integer.valueOf(thWidth)<=0) throw new ServiceException("列宽必须大于0");
        return toAjax(yqfieldsService.insertyqfields(yqfields));
    }

    @PostMapping("/importTemplate")
    @Log(title = "模板管理基础数据导入模板下载", businessType = BusinessType.DOWNLOAD)
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<YqFields> util = new ExcelUtil<YqFields>(YqFields.class);
        util.importTemplateExcel(response, "基础数据");
    }
    /**
     * 修改基础数据
     */
    //@RequiresPermissionsrmissions("manage:yqfields:edit")
    @Log(title = "模板管理修改基础数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody YqFields yqfields)
    {
        return toAjax(yqfieldsService.updateyqfields(yqfields));
    }

    @PutMapping("/editFieldTypeSelect")
    @Log(title = "模板管理修改基础数据列表下拉", businessType = BusinessType.UPDATE)
    public AjaxResult editFieldTypeSelect(@RequestBody YqFields yqfields)
    {
        return toAjax(yqfieldsService.updateyqfields(yqfields));
    }

    /**
     * 删除基础数据
     */
    //@RequiresPermissionsrmissions("manage:yqfields:remove")
    @Log(title = "模板管理删除基础数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable String[] IDs)
    {
        return toAjax(yqfieldsService.deleteyqfieldsByIDs(IDs));
    }

    //@RequiresPermissionsrmissions("manage:tenant:import")
    @Log(title = "模板管理基础数据导入", businessType = BusinessType.DELETE)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport, String type) throws Exception
    {
        ExcelUtil<YqFields> util = new ExcelUtil<>(YqFields.class);
        InputStream in=file.getInputStream();
        List<YqFields> userList = util.importExcel(in);
        in.close();
        String operName = SecurityUtils.getUsername();
        String message = yqfieldsService.importUser(userList, updateSupport, operName,type);
        return success(message);
    }
}
